<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <link rel="stylesheet" href="/component/pear/css/pear.css" />
</head>
<body>
<form class="layui-form" action="">
    <div class="mainBox">
        <div class="main-container">
            <div class="main-container">
                <div class="layui-form-item">
                    <label class="layui-form-label">账号</label>
                    <div class="layui-input-block">
                        <input type="text" name="userName" lay-verify="required" autocomplete="off"  placeholder="请输入账号" class="layui-input">
                    </div>

<!--                    <div class="layui-input-block">-->
<!--                        <input type="text" id="userName" th:value="${myUser.userName}" name="userName" lay-verify="required" placeholder="请输入用户名" autocomplete="off" class="layui-input">-->
<!--                    </div>-->
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">姓名</label>
                    <div class="layui-input-block">
                        <input type="text"  name="nickName"  autocomplete="off" placeholder="请输入姓名" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">邮箱</label>
                    <div class="layui-input-block">
                        <input type="text"  name="email" autocomplete="off" placeholder="请输入邮箱" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">密码</label>
                    <div class="layui-input-block">
                        <input type="text"  name="password" lay-verify="required" autocomplete="off" placeholder="请输入密码" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">电话</label>
                    <div class="layui-input-block">
                        <input type="text"  name="phone" autocomplete="off" placeholder="请输入电话" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">类型</label>
                    <div class="layui-input-block">
                        <input type="radio" name="status" value=true title="激活" checked>
                        <input type="radio" name="status" value=false title="禁用">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">
                        部门
                    </label>
                    <div class="layui-input-block">
                        <input type="number" id="deptId" name="deptId"  lay-verify="required"
                               style="display:none;width: 0px" autocomplete="off" class="layui-input">
                        <ul id="dataTree" class="dtree" data-id="0"></ul>
                    </div>
                </div>
                <input type="text" id="roleId"  name="roleId" style="display:none;"/>
                <div class="layui-form-item">
                    <label class="layui-form-label">角色</label>
                    <div class="layui-input-block show-role-container">
                    </div>
                </div>
            </div>
        </div>
    </div>
    <div class="bottom">
        <div class="button-container">
            <button type="submit" class="pear-btn pear-btn-primary pear-btn-sm" lay-submit="" lay-filter="user-save">
                <i class="layui-icon layui-icon-ok"></i>
                提交
            </button>
            <button type="reset" class="pear-btn pear-btn-sm">
                <i class="layui-icon layui-icon-refresh"></i>
                重置
            </button>
        </div>
    </div>
</form>
<script src="/component/layui/layui.js"></script>
<script src="/component/pear/pear.js"></script>
<script>
layui.use(['form','jquery','dtree'],function(){
    let form = layui.form;
    let $ = layui.jquery;
    let dtree = layui.dtree;
    // 初始化部门树
    dtree.render({
        elem: "#dataTree",
        initLevel: "1",//默认展开层级为1
        width: "100%",
        method: 'get',
        dataStyle: "layuiStyle",  //使用layui风格的数据格式
        // response:{message:"msg",statusCode:200},  //修改response中返回数据的定义
        url: "/dept/buildDeptAll",
        dataFormat: "list",  //配置data的风格为list
        select: true, //指定下拉树模式
        selectTips: "不选默认是顶级目录",
        selectCardHeight: "150"
    });
    dtree.on("node('dataTree')", function (obj) {
        var param = dtree.getNowParam("dataTree");
        $("#deptId").val(param.nodeId);
    });
    //获取所有的角色列表
    getAllRole(function(){
        layui.form.render('radio')
    });
    form.on('submit(user-save)', function(data){
        var rolelRadio = $("[name=roleId]:checked");
        if(rolelRadio == undefined || rolelRadio.length <= 0){
            layer.alert("请给该用户设置角色", {icon: 5});
            return false;
        }
        let loading = layer.load();
        $.ajax({
            url:'/api/user/save',
            data:JSON.stringify(data.field),
            dataType:'json',
            contentType: "application/json;charset=utf-8",
            type:'post',
            success:function(result){
                layer.close(loading);
                if(result.code == 200){
                    layer.msg(result.message,{icon:1,time:1800},function(){
                        parent.layer.close(parent.layer.getFrameIndex(window.name));//关闭当前页
                        parent.layui.table.reload("testReload");
                    });
                }else{
                    layer.msg(result.message,{icon:2,time:1800});
                }
            }
        })
        return false;
    });
    //获取所有的角色列表
    function getAllRole(callback){
        $.ajax({
            url:"/api/role/all",
            type:"GET",
            dataType:'json',
            success:function(result){
                var html = "";
                if(result.data && result.data.length > 0){
                    for(i in result.data){
                        html += '<input type="radio"  name="roleId" value="'+result.data[i].role_id+'" title="'+result.data[i].role_name+'" >' +
                            '<div class="layui-unselect layui-form-radio layui-form-radioed"><i class="layui-anim layui-icon"></i><div>'+result.data[i].role_name+'</div></div>';
                    }
                    $(".show-role-container").html(html);
                    callback();
                }
            }
        });
    }
})
</script>
<script>
</script>
</body>
</html>